<?php

//returns the singleton database object set up in config
function database()
{
	if (isset($_GLOBALS['database'])) return $_GLOBALS['database'];
	
	global $config;
	
	require_once('classes/db.php');
	$_GLOBALS['database'] = new DB($config['db_server'], $config['db_name'], $config['db_user'], $config['db_pass']);
	return $_GLOBALS['database'];
	
}


function current_user()
{
	if (!isset($_GLOBALS['current_user']))
	{
		require_once 'classes/models/user.php';
		if(!array_key_exists('user_id', $_SESSION))
		{
			$_GLOBALS['current_user'] = new User;
		}
		else
		{
			$_GLOBALS['current_user'] = new User($_SESSION['user_id']);
		}
	}
	return $_GLOBALS['current_user'];
}

function set_global_default($key, $value)
{
	if(!array_key_exists($key, $_POST) || !($_POST[$key]))
	{ 
		$_POST[$key] = $value;
	}
	if(!array_key_exists($key, $_GET) || !($_GET[$key]))
	{
	 	$_GET[$key] = $_POST[$key];
	}
}

function redirect($url)
{
	header( 'Location: '. $url ) ;
}
//classes that need to be loaded
require_once('classes/model.php');
require_once('classes/model_factory.php');
require_once('classes/view.php');
require_once('classes/controller.php');


?>